import java.util.Arrays;

public class MyArrayList implements IList{
    // 存储数据
    private int[] elem;
    // 记录数组中有效的数据个数
    private int useSize;



    public MyArrayList(){
        elem = new int[Constant.DEF_CAPACITY];
    }

    // 将顺序表的底层容量设置为initCapacity
    public MyArrayList(int initCapacity){
        elem = new int[initCapacity];
    }

    private boolean full(){
        if(elem.length == useSize){
            return true;
        }else {
            return false;
        }

    }

    public void grow(){

        elem = Arrays.copyOf(elem, elem.length*2);
    }

    @Override
    public void add(int data) {
        // 1.判断顺序表满了吗, 满了就扩容
        if(full()){
            grow();
        }

        // 2.添加数据
        elem[useSize++] = data;

    }

    private void checkPosAdd(int pos, String msg){
        if(pos < 0 || pos > useSize){
            throw new IllegalityPosException(msg);
        }
    }

    @Override
    public void add(int pos, int data) {
        // 1. 判断pos合不合法
        String msg = Constant.ADD_ERROR_POS + pos;
        checkPosAdd(pos, msg);
        // 2. 判断顺序表满了吗
        if(full()){
            grow();
        }
        // 3. 添加数据

        for (int i = useSize-1; i >= pos; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        useSize++;

    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    private boolean isEmpty(){
        return useSize == 0;
    }

    @Override
    public int get(int pos) {
        // 1. 判断顺序表是否为空
        if(isEmpty()){
            throw new EmptyArrayListException(Constant.EMPTY_ARRAYLIST);
        }
        // 2. 判断pos是否合法
        String msg = Constant.ADD_ERROR_POS + pos;
        checkPos(pos, msg);

        // 获取数据
        return elem[pos];
    }

    private void checkPos(int pos, String msg){
        if(pos < 0 || pos >= useSize){
            throw new IllegalityPosException(msg);
        }
    }

    @Override
    public void set(int pos, int value) {
        // 1. 判断顺序表是否为空
        if(isEmpty()){
            throw new EmptyArrayListException(Constant.EMPTY_ARRAYLIST);
        }
        // 2. 判断pos是否合法
        String msg = Constant.SET_ERROR_POS + pos;
        checkPos(pos, msg);

        elem[pos] = value;
    }

    private void checkPosRemove(int pos, String msg){
        if(pos < 0 || pos >= useSize){
            throw new IllegalityPosException(msg);
        }
    }

    @Override
    public int remove(int toRemove) {
        // 1. 判断顺序表是否为空
        if(isEmpty()){
            throw new EmptyArrayListException(Constant.EMPTY_ARRAYLIST);
        }

        // 2. 获取下标
        int pos = indexOf(toRemove);
        if(pos == -1){
            System.out.println("没有你要删除的下标");
            return -1;
        }

        // 3. 删除
        for (int i = pos; i < useSize-1; i++) {
            elem[i] = elem[i+1];
        }

        // elem[useSize] = null; // 如果是引用类型, 需要手动置为空
        useSize--;

        return toRemove;
    }

    @Override
    public int size() {
        return useSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < useSize; i++) {
            elem[i] = 0;
            // elem[i] = null;
        }
        useSize = 0;
    }

    @Override
    public void dispaly() {
        for (int i = 0; i < useSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }
}
